package com.education.wisdom.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.utils.LogUtil;
import com.education.wisdom.pojo.StudentClass;
import com.education.wisdom.pojo.StudentWork;
import com.education.wisdom.pojo.TeacherClass;
import com.education.wisdom.pojo.TeacherWork;
import com.education.wisdom.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.education.common.core.BaseController.writeJSON;


/**
 * 学生提交作业 信息操作处理
 *
 * @author ruoyi
 * @date 2019-06-20
 */
@Controller
@CrossOrigin
@Slf4j
@ResponseBody
@Api(tags = {"学生作业"})
@RequestMapping("/web/studentWork")
public class FullStudentWorkController {

    @Autowired
    private IStudentWorkService studentWorkService;
    @Autowired
    private ITeacherWorkService teacherWorkService;
    @Autowired
    private IStudentClassService studentClassService;
    @Autowired
    private ITeacherClassService teacherClassService;
    @Autowired
    private ISysAppLoginService sysAppLoginService;
    /**
     * 学生查看有哪些作业
     *
     * @param response
     * @param classId  班级ID
     * @param pageSize
     * @param pageNum
     * @throws IOException
     */
    @GetMapping("list")
    @ApiOperation("学生端查看作业接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "classId", value = "班级ID", required = true, dataType = "Integer")})
    public void list(HttpServletResponse response, Integer classId, Integer pageSize, Integer pageNum) throws IOException {
        log.info(LogUtil.LogInfo("学生端查看作业接口", new Date()));
        PageHelper.startPage(pageNum, pageSize);
        List<TeacherWork> list = teacherWorkService.selectByClassId(classId);
        writeJSON(new PageInfo<>(list), response, "200", "success");
    }
    /**
     * 学生查看有哪些作业
     *
     * @param response
     * @param pageSize
     * @param pageNum
     * @throws IOException
     */
    @GetMapping("findByStudengIdlist")
    @ApiOperation("学生端查看作业接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "学生ID", required = true, dataType = "Integer")})
    public void findByStudengIdlist(HttpServletResponse response, String userId, Integer pageSize, Integer pageNum) throws IOException {
        log.info(LogUtil.LogInfo("学生端查看作业接口", new Date()));
//        PageHelper.startPage(pageNum, pageSize);
        StudentClass studentClasss=studentClassService.selectStudentClassById(userId);
        TeacherClass teacherClass=new TeacherClass();
        teacherClass.setClassId(studentClasss.getClassId());
        List<TeacherClass> tclss=teacherClassService.selectTeacherClassList(teacherClass);
        if(tclss.size()>0){
            PageHelper.startPage(pageNum, pageSize);
            List<TeacherWork> list=teacherWorkService.selectByUserId(tclss.get(0).getTeacherId());
            if(list.size()>0){
                writeJSON(new PageInfo<>(list), response, "200", "success");
            }else{
                writeJSON(null, response, "300", "数据为空");
            }
        }else{
            writeJSON(null, response, "300", "教师为空");
        }
    }
    /**
     * 查看提交作业详情
     *
     * @param response
     * @param tworkId  教师作业ID
     * @param userId
     * @throws IOException
     */
    @GetMapping("selectOne")
    @ApiOperation("查看提交作业详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "tworkId", value = "老师发布作业ID", required = true, dataType = "String")})
    public void selectOne(HttpServletResponse response, Integer tworkId, String userId) throws IOException {
        log.info(LogUtil.LogInfo("查看提交作业详情", new Date()));
        Map<Object, Object> map = new HashMap<>();
        map.put("tworkId", tworkId);
        map.put("userId", userId);
        StudentWork studentWork = studentWorkService.selectDetailsByMap(map);
        writeJSON(studentWork, response, "200", "success");
    }

    /**
     * 学生端查看老师作业详情
     *
     * @param response
     * @param id       教师作业ID
     * @throws IOException
     */
    @GetMapping("teacherJobDetails")
    @ApiOperation("学生端查看老师作业详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "tworkId", value = "老师发布作业ID", required = true, dataType = "String")})
    public void teacherJobDetails(HttpServletResponse response, Integer id) throws IOException {
        log.info(LogUtil.LogInfo("学生端查看老师作业详情", new Date()));
        TeacherWork teacherWork = teacherWorkService.selectTeacherWorkById(id);
        writeJSON(teacherWork, response, "200", "success");
    }

    /**
     * 学生端提交作业
     *
     * @param response
     * @param studentWork
     * @throws IOException
     */
    @GetMapping("submitHomework")
    @ApiOperation("学生端提交作业接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "title", value = "学生作业标题", required = true, dataType = "String"),
            @ApiImplicitParam(name = "url", value = "附件资源路径", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "classId", value = "班级ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userName", value = "用户名字", required = true, dataType = "String"),
            @ApiImplicitParam(name = "imgUrl", value = "图片资源路径", required = true, dataType = "String"),
            @ApiImplicitParam(name = "urlName", value = "附件资源名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "content", value = "内容文本", required = true, dataType = "String"),
            @ApiImplicitParam(name = "tworkId", value = "老师发布作业ID", required = true, dataType = "String")})
    public void submitHomework(HttpServletResponse response, StudentWork studentWork) throws IOException {
        log.info(LogUtil.LogInfo("学生端提交作业接口", new Date()));
        Map<Object, Object> map = new HashMap<>();
        map.put("tworkId", studentWork.getTworkId());
        map.put("userId", studentWork.getUserId());
        StudentWork sxStudentWork = studentWorkService.selectDetailsByMap(map);
        if (sxStudentWork == null) {
            //根据userId 设置班级id
            Integer classId = studentClassService.selectStudentClassById(studentWork.getUserId()).getClassId();
            String userRelname = sysAppLoginService.selectSysAppLoginById(studentWork.getUserId()).getUserRelname();
            studentWork.setUserName(userRelname);
            studentWork.setClassId(classId);
            studentWork.setCreateTime(new Date());
            studentWorkService.insertStudentWork(studentWork);
            writeJSON("提交成功", response, "200", "success");
        } else {
            writeJSON("已提交过作业", response, "201", "success");
        }
    }

}
